﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.Contract
{
    public class CargoownerContractRepository : ICargoownerContractRepository
    {
        private readonly LogisticsDbContext db;
        public CargoownerContractRepository(LogisticsDbContext _db)
        {
            db = _db;
        }
        public async Task<int> Create(Cargoowner ca)
        {
            var list= await db.Cargoowner.AddAsync(ca);
            var cargoownertrack = new Contracttracking
            {
                ContractId=list.Entity.Id.ToString(),
                Abnormal=1,
                Condition="未发货"
            };
            db.Contracttracking.Add(cargoownertrack);
            return db.SaveChanges();
        }

        public async Task<int> Delete(int Id)
        {
            var carry = await db.Carry.FindAsync(Id);
            db.Carry.Remove(carry);
            return db.SaveChanges();
        }

        public async Task<Cargoowner> GetCargoownerById(int Id)
        {
            var ca = await db.Cargoowner.FindAsync(Id);
            return ca;
        }

        public Task<List<Cargoowner>> GetCargoowners()
        {
            string sql = "select b.`Name` OName,c.`Name` FName,d.`Name` LName,a.* from carry a,Owner b,field c,line d where a.OutsourcingUnit=c.Id and a.OutsourcingLeader=b.Id and a.Line=d.Id";
            var carr = db.Carry.ToList();
            var file = db.Field.ToList();
            var lin = db.Line.ToList();
            var own = db.Owner.ToList();
            var list = from a in carr
                       join b in own on a.OutsourcingLeader equals b.Id
                       join c in file on a.OutsourcingUnit equals c.Id
                       join d in lin on a.Line equals d.Id
                       select new
                       {
                           a.Id,
                           a.NumberId,
                           a.ContractTitle,
                           a.OutsourcingUnit,
                           a.OutsourcingLeader,
                           a.Line,
                           a.Freightrate,
                           a.CharterConditions,
                           a.CharterAmount,
                           a.SigningDate,
                           a.Handler,
                           a.ProjectDescription,
                           a.ChangeClause,
                           a.Enclosure,
                           a.CreationTime,
                           OName = b.Name,
                           FName = c.Name,
                           LName = d.Name,
                       };
            return (Task<List<Cargoowner>>)list;
            //using (System.Data.Common.DbConnection conn = new("Server=10.31.64.9;Port=3306;Database=Logistics;Uid=si;Pwd=111111;"))
            //{
            //    conn.Open();
            //    var result = (List<Carry>)await conn.(sql);
            //    return result;
            //}
            //var list= db.Carry.ToList();
        }

        public async Task<int> Update(Cargoowner ca)
        {
            var list = await db.Cargoowner.FindAsync(ca.Id);
            list = ca;
            db.Entry(list).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
